package fan.codeGenerate.generate;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import fan.codeGenerate.analysisXML.AnalysisXML;
import fan.codeGenerate.common.GenerateInfo;
import fan.codeGenerate.databse.ConnectOracle;

/** 
* @author liu_fan 2017年3月30日 上午10:52:30  
*/
public class Generate {
	private Map<String, Object> jdbcInfo = null;
	private List<Map<String, Object>> generateList = null;
	//初始化时，获取配置文件信息
	public Generate(String generatePath){
		AnalysisXML ana = new AnalysisXML(generatePath);
    	jdbcInfo = ana.getJdbcInfo();
    	generateList = ana.getGenerateList();
	}
	//根据表名，获取表内字段信息，并返回
	public List<Map<String,Object>> getTableInfo(String tableName){
		List<Map<String,Object>> tableList = new ArrayList<Map<String,Object>>();
		ConnectOracle conOra = new ConnectOracle(jdbcInfo);
		StringBuilder sql = new StringBuilder();
		sql.append(" select ");
		sql.append(" column_name,data_type,data_length,nullable,column_id ");
		sql.append(" from user_tab_cols ");
		sql.append(" where table_name = upper('").append(tableName).append("') ");
		sql.append(" and user_generated = upper('YES') ");
		sql.append(" order by column_id asc ");
    	tableList = conOra.executeSelect(sql.toString());
    	conOra.close();
    	return tableList;
	}
	//根据表明和生成路径，生成代码
	public void generateByTable(String tableName, String generatePath){
		//根据表明，获取表内信息
		List<Map<String,Object>> tableList = this.getTableInfo(tableName);
		
		GenerateInfo genInfo = new GenerateInfo("src/test/java/",generatePath,tableName);
		
		//根据表字段和生成路径，生成文件
		GenerateFile genEn = new GenerateFile();
		genEn.generateEntity(tableList,genInfo);
		
	}
	//生成代码
	public void generateCodes(){
		//循环生成表信息
		for(Map<String,Object> map : generateList){
			//生成代码
			this.generateByTable(map.get("tableName").toString(),
					map.get("generatePath").toString());
		}
	}	
}
